home *** CD-ROM | disk | FTP | other *** search
- #ifdef lint
- static char *SCCSid = "%W% (NCSA) %G%";
- #endif
- /*
- * netevent.c
- * by Gaige B. Paulsen
- ****************************************************************************
- * Part of NCSA Telnet for the Macintosh *
- * *
- * Uses : *
- * TCP/IP kernel for NCSA Telnet *
- * by Tim Krauskopf *
- * with Macintosh code by Gaige B. Paulsen *
- * *
- * National Center for Supercomputing Applications *
- * 152 Computing Applications Building *
- * 605 E. Springfield Ave. *
- * Champaign, IL 61820 *
- * *
- * *
- ****************************************************************************
- *
- * Network event handler for NCSA Telnet for the Macintosh
- *
- * Called by:
- * event.c
- * maclook.c
- */
-
- /*
- * Files for inclusion.
- */
- #include <stdio.h>
- #include <string.h>
-
- #include <Dialogs.h>
- #include <Events.h>
- #include <Memory.h>
- #include <Windows.h>
- #include <Menus.h>
-
- #include "hostform.h"
- #include "whatami.h"
- #include "wind.h"
- #include "configrec.h"
- #include "util.h"
- #include "bkgr.h"
- #include "maclook.h"
- #include "user.h"
- #include "menu.h"
- #include "tools.h"
-
- #include "rsmac.h"
- #include "vrrgmac.h"
- #include "tekrgmac.h"
- #include "vsdata.h"
- #include "vskeys.h"
-
- #include "Prefs.h"
-
- #define HFTP 21 /* BYU mod */
-
- #define Conn 5
- #define COnext 1
- #define OopsDLOG 640
- #define PASTELEFT 130 /* UserEvent, PASTELEFT causes pasteText */
- #define PASTESIZE 32 /* BYU 2.4.7 - Paste in small chunks */
-
- int
- blocksize, /* how large do we make the blocks when we read? */
- ev, /* Event type */
- what, /* What kind of net event */
- dat; /* The data for the event */
-
- extern WindRec
- *screens, /* The screen array from Maclook */
- ftplog; /* The FTP log screen from Maclook */
-
- extern unsigned char
- tempspot[]; /* 256 bytes for temp strings */
-
- unsigned char *readspace; /* main buffer space */
-
- extern void DoTheMenuChecks();
-
- extern short slip_connection; /* BYU 2.4.16 */
-
- extern int
- MacBinary, /* MacBinary status from menu.c, for resetting if prefs say so.. */
- ScrlLock, /* Hey, do we have scroll lock? */
- scrn, /* The current screen from Maclook */
- numwindows; /* The number of windows now from Maclook */
- extern void /* BYU */
- ftpgo(), /* BYU */
- ftppi(), /* BYU */
- userftpd(); /* BYU */
- char *errmsg,
- *neterrstring();
- pascal int
- modalProc();
- char ftpname[128];
- int portNo;
- long ftptime;
- #ifndef MPW
- long TickCount();
- #endif
- extern MenuHandle myMenus[];
-
- int FileInTransit;
-
- void NetError
- (
- int code
- )
- {
- char buffer1[100],buffer2[100], *neterrstring();
- int ev,what,class;
- short dItem;
- DialogPtr dtemp;
-
- buffer1[0]=0;
- buffer2[0]=0;
-
- if (code<0) {
- strncpy( buffer2, neterrstring(code), 99);
- ev = Sgetevent( ERRCLASS,&what,&code);
- if (ev)
- strncpy( buffer1, neterrstring(code), 99);
- }
- else
- strncpy( buffer1, neterrstring(code), 99);
-
- class = code/100;
- if ((class==1 || class==5 || class==9 ) && ( code!=103)) {
- /* Ok, I'll show the user these */
- #ifndef MPW
- paramtext( ctop(buffer1),ctop(buffer2),"\P ","\P ");
- #else
- paramtext( buffer1,buffer2," "," ");
- #endif
- dtemp=GetNewDialog(OopsDLOG,(Ptr) 0L,(WindowPtr) -1L); /* Put up the error notice */
- ModalDialog((ModalFilterProcPtr) modalProc,&dItem); /* Get an event */
- DisposDialog(dtemp); /* Take it off.....all off......*/
- }
- else {
- if (buffer1[0])
- putln(buffer1);
- if (buffer2[0])
- putln(buffer2);
- }
-
- }
-
- void RangeError
- (
- int i
- )
- {
- char temp[20];
-
- sprintf(temp,"%d in %d", -i,numwindows);
- putln(temp);
- }
-
- int WindByPort
- (
- int port
- )
- {
- int i=0;
-
- while (i<numwindows && (screens[i].port!=port || screens[i].active <0)) i++;
-
- if (i>=numwindows) {
- i = 0; /* BYU */
- while ((i<numwindows) && /* BYU */
- ((screens[i].ftpport!=port) || /* BYU */
- (screens[i].active <0))) i++; /* BYU */
- if (i>=numwindows) { /* BYU */
- putln("Can't find a window for the port # in WindByPort"); /* BYU */
- RangeError(i); /* BYU */
- if (i==0) i=999; /* BYU */
- return(-i); /* BYU */
- } /* BYU */
- } /* BYU */
-
- return(i);
- }
-
- int WindByDom
- (
- int port
- )
- {
- int i=0;
-
- while (i<numwindows && (screens[i].port!=port || screens[i].active >=0)) i++;
-
- if (i>=numwindows) {
- if (i==0) i=999;
- putln("CANT FIND THE domPORT SPECIFIED");
- return(-i);
- }
-
- return(i);
- }
-
- void FlushNetwork
- (
- int scrn
- )
- {
- int throwsize,cnt=512;
-
- if (blocksize < 512)
- throwsize = 512;
- else
- throwsize = blocksize;
-
- RSskip(screens[scrn].vs, 1); /* Don't do any output */
-
- while (cnt>0) {
- cnt = netread(dat,readspace,throwsize);
- parse( &screens[scrn ], readspace, cnt);
- }
-
- RSskip(screens[scrn].vs, 0); /* Do output now */
- SetPort( screens[scrn].wind);
- InvalRect(&screens[scrn].wind->portRect); /* Don't forget to redraw */
- }
-
-
- ftppasteText( scrn) /* BYU */
- int scrn; /* BYU */
- { /* BYU */
- char *ascii; /* BYU */
- /* BYU */
- ascii = screens[scrn].outptr; /* BYU */
- while ((screens[scrn].outlen > 0) && /* BYU */
- (*ascii>31) && (*ascii <127)) { /* BYU */
- parse( &screens[ scrn], ascii, 1); /* BYU */
- screens[scrn].kbbuf[ screens[scrn].kblen++ ] = *ascii; /* BYU */
- screens[scrn].outlen--; /* BYU */
- ascii++; /* BYU */
- } /* BYU */
- screens[scrn].outptr = ascii; /* BYU */
- if (*ascii == '\015') { /* BYU */
- parse( &screens[scrn],"\015\012",2); /* BYU */
- screens[scrn].kbbuf[ screens[scrn].kblen++ ] = 0; /* BYU */
- ftppi(screens[scrn].kbbuf); /* BYU - ftp client */
- screens[scrn].kblen=0; /* BYU */
- } /* BYU */
- while ((screens[scrn].outlen > 0) && /* BYU */
- ((*ascii<=31) || (*ascii >=127))) { /* BYU */
- screens[scrn].outlen--; /* BYU */
- screens[scrn].outptr++; /* BYU */
- } /* BYU */
- } /* BYU */
-
-
- void pasteText
- (
- int scrn
- )
- {
- int amount;
- if (!screens[scrn].outlen)
- return;
-
- /* BYU 2.4.7 - Need to avoid the Macintosh sending more than the host
- can handle, so wait for two cycles (back to this routine) of no
- input from the host before each chunk of the paste buffer is sent.
- */
- if ((screens[scrn].connectionType == 0) || slip_connection) { /* BYU 2.4.16 */
-
- if (netpush(screens[scrn].port) != 0) { /* BYU 2.4.16 - wait until not busy */
- netputevent( USERCLASS, PASTELEFT, scrn); /* BYU 2.4.16 */
- return; /* BYU 2.4.16 */
- } /* BYU 2.4.16 */
- if (screens[scrn].incount) { /* BYU 2.4.16 */
- screens[scrn].incount = 0; /* BYU 2.4.16 */
- screens[scrn].outcount = 0; /* BYU 2.4.16 */
- netputevent( USERCLASS, PASTELEFT, scrn); /* BYU 2.4.16 */
- return; /* BYU 2.4.16 */
- } /* BYU 2.4.16 */
- if (screens[scrn].outcount < 2) { /* BYU 2.4.16 */
- screens[scrn].outcount++; /* BYU 2.4.16 */
- netputevent( USERCLASS, PASTELEFT, scrn); /* BYU 2.4.16 */
- return; /* BYU 2.4.16 */
- }
- if (netqlen(screens[scrn].port) > 0) { /* BYU 2.4.16 - wait until not full */
- netputevent( USERCLASS, PASTELEFT, scrn); /* BYU 2.4.16 */
- return; /* BYU 2.4.16 */
- } /* BYU 2.4.16 */
-
- } /* BYU 2.4.16 */
-
- if (screens[scrn].ftpstate != 0) /* BYU */
- ftppasteText( scrn); /* BYU */
- else { /* BYU */
- #if 0 /* BYU - VAX/VMS systems can't handle large pastes at the time of this patch. */
- amount = netwrite( screens[scrn].port, /* BYU */
- screens[scrn].outptr, /* BYU */
- screens[scrn].outlen); /* BYU */
- #else /* BYU */
- if (PASTESIZE <= screens[scrn].outlen) /* BYU */
- amount = PASTESIZE; /* BYU */
- else /* BYU */
- amount = screens[scrn].outlen; /* BYU */
- amount = netwrite( screens[scrn].port, /* BYU */
- screens[scrn].outptr, /* BYU */
- amount); /* BYU */
- #endif /* BYU */
- if (screens[scrn].echo) /* BYU */
- parse( &screens[scrn], screens[scrn].outptr,amount); /* BYU */
- screens[scrn].outlen -= amount; /* BYU */
- screens[scrn].outptr += (long) amount; /* BYU */
- } /* BYU */
-
- if ( screens[scrn].outlen <=0) {
- screens[scrn].clientflags &= ~PASTE_IN_PROGRESS; /* BYU 2.4.7 */
- HUnlock(screens[scrn].outhand);
- DisposHandle(screens[scrn].outhand);
- (char *) screens[scrn].outhand = screens[scrn].outptr = (char *) 0L;
- }
- else
- netputevent( USERCLASS, PASTELEFT, scrn);
- }
-
-
-
-
-
-
-
-
-
-
-
- void DoNetEvents
- (
- void
- )
- {
- int i,cnt;
-
- ev = Sgetevent(USERCLASS | CONCLASS | ERRCLASS,&what,&dat);
-
- if ( !ev ) {
- if ( (ftptime < TickCount()) && FileInTransit) {
- long pos;
-
- Sftpstat(&pos); /* get transfer status */
- if (pos <= 0)
- ftpmess("FTP Status: transferring\015\012");
- else {
- if (FileInTransit+2)
- sprintf(tempspot,"FTP Status: %ld bytes remaining.\015\012", pos);
- else
- sprintf(tempspot,"FTP Status: %ld bytes transferred.\015\012", pos);
-
- ftpmess(tempspot);
- }
-
- ftptime=TickCount()+30; /* 1/2 second intervals */
- }
- return;
- }
-
- switch( what) {
- case ERRCLASS: /* Error event */
- NetError(dat);
- break;
-
- case CONCLASS: /* Connection type event */
- switch (ev) {
- case CONOPEN: /* connection opened or closed */
- #ifdef JOHN_DEERE
- { extern int jdp;
- if (jdp == dat) {
- netwrite( jdp, "Hello from Macintosh\015\012",22); /* BYU 2.4.18 - changed \n to \015 */
- netclose( jdp);
- jdp = netlisten( 2001,0); /* BYU 2.4.15 */
- }
- }
- #endif
- i=WindByPort(dat);
- if (i<0) { RangeError(i); return; }
- /* BYU mod - This tests TRUE if it is the telnet port or the main ftp port */
- if (dat == screens[i].port) { /* BYU - is this Telnet? */
- screens[ i].active=0; /* BYU - Mark active */
- RSshow( screens[i].vs); /* BYU */
- SelectWindow(screens[i].wind); /* BYU */
- #if 1 /* BYU 2.4.20 */
- if (screens[i].ftpstate != 0) /* BYU 2.4.20 - Only if not FTP */
- userftpd(CONOPEN,dat); /* BYU */
- #else /* BYU 2.4.20 */
- if (screens[i].ftpstate == 0) { /* BYU - Only if not FTP */
- netpush(screens[i].port); /* BYU */
- netwrite(screens[i].port,"\377\375\001\377\375\003\377\374\43",9); /* BYU - Default telnet parms */
- } else { /* BYU */
- userftpd(CONOPEN,dat); /* BYU */
-
- } /* BYU */
- #endif /* BYU 2.4.20 */
- screens[i].Usga=1; /* BYU */
- changeport(scrn,i); /* BYU */
- liveport(scrn); /* BYU */
- DoTheMenuChecks(); /* BYU */
- } else { /* BYU - not Telnet so must be FTP */
- userftpd(CONOPEN,dat); /* BYU */
- } /* BYU */
- break;
-
- case CONDATA: /* data arrived for me */
- i=WindByPort(dat); /* BYU */
- if (i<0) { RangeError(i); return; } /* BYU */
- if (ScrlLock || !screens[ WindByPort(dat)].enabled)
- netputuev( CONCLASS, CONDATA, dat);
- else {
- if (screens[i].ftpstate == 0) { /* BYU */
- cnt = netread(dat,readspace,blocksize); /* BYU */
- parse( &screens[ WindByPort( dat)], readspace, cnt); /* BYU */
- screens[i].incount += cnt; /* BYU 2.4.7 */
- } /* BYU */
- else { /* BYU */
- userftpd(CONDATA,dat); /* BYU */
- } /* BYU */
- }
- break;
-
- case CONFAIL:
- {
- int i;
- netclose( dat);
- i= WindByPort(dat);
- if (i<0) { RangeError(i); return; }
- #ifndef MPW
- OtherError( "\PHost or gateway not responding:",
- screens[i].machine);
- #else
- OtherError( "Host or gateway not responding:",
- screens[i].machine);
- #endif MPW
- destroyport( i);
- }
- break;
-
- case CONCLOSE:
- {
- int i;
-
- i= WindByPort(dat);
- if (i<0) {
- netclose( dat); /* We close again.... */
- RangeError(i);
- return;
- }
- /* BYU - This tests TRUE if it is the telnet port or the main ftp port */
- if (dat == screens[i].port) { /* BYU */
- putln("Closing...."); /* BYU */
- FlushNetwork(i); /* BYU */
- netclose( screens[i].port); /* BYU */
- removeport(i); /* BYU */
- } /* BYU */
- else { /* BYU */
- userftpd(CONCLOSE,dat); /* BYU */
- } /* BYU */
- }
- break;
-
- default:
- break;
- }
- break;
-
- case USERCLASS:
- switch (ev) {
- case DOMOK: /* domain worked */
- {
- struct machinfo *mp;
- int i;
-
- i = WindByDom( dat);
- if (i<0) { RangeError(i); return; }
- mp = Slooknum( dat); /* get machine info */
- if (screens[i].ftpstate != 0) /* BYU - ftp client */
- screens[ i].port = Snetopen(mp,HFTP,0); /* BYU 2.4.15 - open to host name */
- else /* BYU */
- screens[ i].port = Snetopen(mp,mp->port,0);/* BYU 2.4.15 - open to host name */
- if (screens[i].port <0) { /* Handle Snetopen fail */
- destroyport( i);
- }
- screens[i].active = 1; /* Change real status */
- opnport( i); /* Change status mark */
- }
- break;
- case DOMFAIL: /* domain failed */
- {
- i= WindByDom(dat);
- if (i<0) { RangeError(i); return; }
- #ifndef MPW
- OtherError( "\PHost not in hosts file or on server:",
- screens[i].machine);
- #else
- OtherError( "Host not in hosts file or on server:",
- screens[i].machine);
- #endif MPW
- destroyport( i);
- }
- break;
- case FTPBEGIN:
- Sftpname(ftpname); /* get name */
- /* ftpmess("\015\012FTP transferring: "); */
- /* ftpmess(ftpname); */
- /* ftpmess("\015\012"); */
- FileInTransit=dat;
- ftptime=TickCount();
- break;
- case FTPLIST:
- ftpmess("FTP Directory being Sent\015\012");
- break;
- case FTPEND:
-
- /* if (!(FileInTransit+2))
- ftpmess("\015\012"); */
- ftpmess("FTP Transfer Concluding\015\012");
- ftpmess("\015\012");
- FileInTransit=0;
- break;
- case FTPCOPEN:
- {
- struct machinfo *mp;
- unsigned char ftpinfo[8];
-
- if (Prefs.MacBReset)
- MacBinary = Prefs.MacBinary;
- updateMenuChecks();
- xferstart();
- ftpmess("-----------------------------------------------------------------------------\015\012");
- ftpmess("FTP server initiated from host: ");
- Sftphost(ftpinfo);
-
- if ((NULL == (mp = Slookip(ftpinfo))) || (NULL == mp->sname))
- sprintf(&tempspot[4],"%d.%d.%d.%d\015\012",
- (int)ftpinfo[0],(int)ftpinfo[1],
- (int)ftpinfo[2],(int)ftpinfo[3]);
- else
- sprintf(&tempspot[4],"%s\015\012",mp->sname);
- ftpmess(&tempspot[4]);
- }
- break;
- case FTPCLOSE:
- xferdone();
- if (Prefs.MacBReset)
- MacBinary = Prefs.MacBinary;
- updateMenuChecks();
- ftpmess("FTP server ending session\015\012");
- ftpmess("-----------------------------------------------------------------------------\015\012");
- ftpmess("\015\012");
-
- break;
-
-
- case FTPUSER:
- ftpmess("FTP user ");
- Sftpuser(tempspot);
- ftpmess(tempspot);
- ftpmess(" logging in\015\012");
- break;
-
- case FTPPWOK:
- ftpmess("FTP password OK - logged in\015\012");
- break;
- case FTPPWNO:
- ftpmess("FTP password failed - access denied\015\012");
- break;
-
- case 128: /* RGredraw event */
- if (VGalive(dat) && RGsupdate(dat))
- TekEnable(dat);
- break;
- case PASTELEFT:
- pasteText(dat);
- break;
- default:
- break;
- }
- break;
- default:
- break;
- }
- } /* DoNetEvents */
-
- void OtherError
- (
- char *mess1,
- char *mess2
- )
- {
- char buffer1[100],buffer2[100];
- short dItem;
- DialogPtr dtemp;
-
- buffer1[0]=0;
- buffer2[0]=0;
-
- strncpy(buffer1, mess1, 99);
- strncpy(buffer2, mess2, 99);
-
- #ifndef MPW
- paramtext( buffer1,buffer2,"\P ","\P ");
- #else
- paramtext( buffer1,buffer2,"P ","P ");
- #endif MPW
-
- dtemp=GetNewDialog(OopsDLOG,(Ptr) 0L,(WindowPtr) -1L); /* Put up the error notice */
- ModalDialog((ModalFilterProcPtr) modalProc,&dItem); /* Get an event */
- DisposDialog(dtemp); /* Take it off.....all off......*/
- }
-
- void ShowAllErrors
- (
- void
- )
- {
- int ev=1, what, dat;
-
- while (ev)
- {
- ev = Sgetevent( ERRCLASS,&what,&dat);
- if (ev)
- NetError(dat);
- }
- }
-
- /*****************************************************************************/
- /* setblocksize()
- * Make sure that we have space for the block of data that is to be read each
- * time the netread() is called.
- */
- int setblocksize
- (
- int tosize
- )
- {
-
- blocksize = tosize; /* keep size of block */
-
- if (tosize < 512) /* minimum buffer */
- tosize = 512;
-
- if (readspace)
- DisposPtr(readspace); /* free old block */
-
- if (NULL == (readspace = NewPtr(tosize + 100)))
- return(-1);
-
- return(0);
- }
-
-
-
-
-
-